package cn.fx.service.impl;

import cn.fx.exception.GlobalException;
import cn.fx.exception.GloobalMadeException;
import cn.fx.service.IVerifyCodeService;
import cn.fx.util.StrUtils;
import cn.fx.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public String getVerifyCode(String key) {
        //校验
        if(StringUtils.isEmpty(key)){
            throw new GloobalMadeException("参数有误！");
        }

        //生成随机验证码
        String code = StrUtils.getComplexRandomString(4);

        //存入redis中，设置过期时间 3分钟
        redisTemplate.opsForValue().set(key,code,3, TimeUnit.MINUTES);

        //生成base64位字符串
        return VerifyCodeUtils.verifyCode(100,40,code);
    }
}
